package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhuizikejithreeig.cn.been.MesBeen;
import com.zhuizikejithreeig.cn.been.RegisterBeen;
import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengPawActivity extends BasicActivity implements View.OnClickListener {

    private TextView mTishi;
    private EditText mName3;
    private Button mName5;
    private Button mName8;
    private String phoneNumber;
    private int mTime;
    private TimerTask mWait;
    private Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_cheng_paw);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitle_V(getResources().getString(R.string.string74));

        mTishi = (TextView) findViewById(R.id.tishi);
        mName3 = (EditText) findViewById(R.id.name3);
        mName5 = (Button) findViewById(R.id.name5);
        mName8 = (Button) findViewById(R.id.name8);

        mName5.setOnClickListener(this);
        mName8.setOnClickListener(this);


        phoneNumber = Utils.getString(ContectUtils.PHONESAVE);

        String start = phoneNumber.substring(0, 3);
        String end = phoneNumber.substring(phoneNumber.length() - 3, phoneNumber.length());
        String text = start + "xxxx" + end;

        mTishi.setText(text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //获取验证码
            case R.id.name5:
                mTime = 60;
                mWait = new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mTime <= 0) {
                                    mName5.setText(getResources().getString(R.string.string49));
                                    mName5.setEnabled(true);
                                    mWait.cancel();
                                } else {
                                    mName5.setText(mTime + "秒后重获");
                                    mName5.setEnabled(false);
                                }
                                mTime--;
                            }
                        });
                    }
                };
                if (isConnected()) {
                    //                    sendPostRequest(trim);
//                    newProject(trim);
                    haveCode(phoneNumber);
                    timer.schedule(mWait, 0, 1000);
                } else {
                    ToastMake.showToast(getResources().getString(R.string.string57));
                    mWait.cancel();
                    mName5.setText(getResources().getString(R.string.string49));
                    mName5.setEnabled(false);
                    return;
                }
                break;

            //提交
            case R.id.name8:
                String trim = mName3.getText().toString().trim();
                if (!TextUtils.isEmpty(trim)) {
                    CompontApplayCation.registCode = trim;
                    skipOtherActivity(ChengPawTwoActivity.class, false);
                }else {
                    ToastMake.showToast("验证码不能为空");
                }
                break;
        }
    }

    private void haveCode(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("tags", "3");
        String json = GsonUtil.parseMapToJson(map);
        OkHttpUtils
                .postString()
                .url(UriContect.GETCHECKCODE)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        mWait.cancel();
                        mName5.setText(getResources().getString(R.string.string49));
                        mName5.setEnabled(true);
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);
                        if (mesBeen == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            mWait.cancel();
                            mName5.setText(getResources().getString(R.string.string49));
                            mName5.setEnabled(true);
                            return;
                        }

                        if (!TextUtils.equals(mesBeen.getError_code(), "0")) {
                            mWait.cancel();
                            ToastMake.showToast(mesBeen.getMsg());
                            mName5.setText(getResources().getString(R.string.string49));
                            mName5.setEnabled(true);
                            return;
                    }
                        }
                    });

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer!=null) {
            timer = null;
        }
        if (mWait!=null){
            mWait.cancel();
            mWait = null;
        }
    }
}
